//
//  LZUserModel.m
//  laziz_Consumer
//
//  Created by ZhaoDongBo on 2016/11/10.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZUserModel.h"
#import "GVUserDefaults+LZProperties.h"
#import "GVUserDefaults+LZProperties.h"
#import "NSString+Additions.h"
#import "LZNotificationPresenter.h"
#import <ShareSDK/ShareSDK.h>
#import "AppDelegate.h"

@implementation LZUserModel

static LZUserModel * userModel = nil;

+ (instancetype)Instance {
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        userModel = [[self alloc] init];
    });
    return userModel;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        
        _api = [[LZUserInfoPresenter alloc] init];
    }
    return self;
}

- (NSString *)userId {
    return [GVUserDefaults standardUserDefaults].userId;
}

- (NSString *)userToken {
    return [GVUserDefaults standardUserDefaults].userToken;
}

//login or logout

- (BOOL)isLogin {
    return (self.userToken && [self.userToken length] > 0);
}

- (void)logOut {
    self.userInfo = nil;
    [GVUserDefaults standardUserDefaults].userId = nil;
    [GVUserDefaults standardUserDefaults].userToken = nil;
    [GVUserDefaults standardUserDefaults].userInfoModel = nil;
    
    //Facebook
    [ShareSDK cancelAuthorize:SSDKPlatformTypeFacebook];
    
    //Twitter
    [ShareSDK cancelAuthorize:SSDKPlatformTypeTwitter];

    // 取消推送别名设置
    [LZNotificationPresenter removeNotificationAlias];
    
    AppDelegate *d = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [d hiddenTabbarBadge];
    //重置首页数据
    [d homeVCChangeDatasourceWhenUserLogout];

}

- (void)saveUserInfo:(LZCurrentUserInfo *)userInfo {
    
    [LZCurrentUserInfo setUserInfoWithModel:userInfo];
}

- (LZCurrentUserInfo *)userInfo {
    
    if (![self isLogin])
        return nil;
    
    LZCurrentUserInfo * userInfo = [LZCurrentUserInfo getUserInfoModel];
    return userInfo;
}

- (void)updateUserInfoIfNeeded{
    if ([self isLogin]) {
        [self.api userGetUserInfoBlockonSuccess:^(id data) {

        } onFail:^(NSString *fail) {

        }];
    }
}

@end
